package com.wtwd.device.access;

import com.wtwd.device.common.RespCode;
import com.wtwd.device.entity.VerifyResult;
import com.wtwd.device.util.SignUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;

/**
 * @author zdl
 * @Description
 * @create 2021-09-26 13:43
 */
public class SignatureAccess {

    public static final String SIGN_FIELD = "sign";
    private static final String APPID_FIELD = "appID";
    private static final String TIMESTAMP_FIELD = "timestamp";

    private static final String APP_SECRET = "wtwd8887";


    private String sign;
    private String appId;
    private String timestamp;


    public SignatureAccess(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        sign = headers.getFirst(SIGN_FIELD);
        appId = headers.getFirst(APPID_FIELD);
        timestamp = headers.getFirst(TIMESTAMP_FIELD);
    }

    public VerifyResult verifyEncrypt() {
        if (StringUtils.isNoneBlank(sign, appId, timestamp)) {
            if (sign.equals(SignUtil.md5(appId + APP_SECRET + timestamp))) {
                return null;
            }
        }
        VerifyResult verifyResult = new VerifyResult();
        verifyResult.setRespCode(RespCode.UNAUTHORIZED);
        return verifyResult;
    }
}
